package com.cn.wanxi.util;

import java.io.*;
import java.util.List;

public class ReadAndWriteIO {
    /**
     * 讀取记事本
     *
     * @param name
     * @return
     */
    public String readNote(String name) {
        File file = new File(Path.PATH.getPath(), name);

        StringBuilder result = new StringBuilder();
        try {
//            InputStream inputStream = new FileInputStream(file);
//            int read = 0;
//            while ((read = inputStream.read()) != -1) {
//                System.out.print((char) read);
//            }
//StringBuilder,StringBuffer,String的区别？
//String aa="aa";
//String bb=new String("bb");
            Reader reader = new FileReader(file);
            int read = 0;
            while ((read = reader.read()) != -1) {
                result.append((char) read);
            }
//            System.out.println(result);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result.toString();
    }

    /**
     * 根据地址和文件名称返回输出流
     *
     * @param name
     * @param list
     */
    public void createNote(String name, List<String> list) {
        File file = new File(Path.PATH.getPath());
        if (!file.exists()) {
            file.mkdirs();
        }
        File file1 = new File(file, name);

        OutputStream outputStream = null;

        try {
            file1.createNewFile();
            outputStream = new FileOutputStream(file1);
            for (String s : list) {
                outputStream.write(s.getBytes());
                outputStream.write(Path.SPLIT.getPath().getBytes());
            }
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
